package com.knife.oauth.api.controller.v1;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knife.common.utils.Results;
import com.knife.interfaces.annotations.KnifeInterfaceLog;
import com.knife.interfaces.constants.KnifeInterfaceCodeEnum;
import com.knife.oauth.api.controller.IamUserRegisterDto;
import com.knife.oauth.api.dto.ForgetPasswordDto;
import com.knife.oauth.app.service.IamUserService;
import com.knife.oauth.domain.entity.IamUser;
import com.knife.oauth.domain.entity.KnifeUserDetails;
import com.knife.oauth.tenant.annonations.KnifeTenant;
import com.knife.oauth.util.DetailHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2022年02月27日 星期日 11:08 上午
 * @description
 * @since 1.0
 */
@RestController("v1.IamUserController")
@RequestMapping("/v1/{organizationId}/iam-user")
@KnifeTenant
public class IamUserController {
    @Autowired
    private IamUserService iamUserService;

    @GetMapping
    public ResponseEntity<KnifeUserDetails> getUserDetails(@PathVariable Long organizationId) {
        return Results.success(DetailHelper.getUserDetails());
    }

    @PostMapping("/change-password")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.CHANGE_PASSWORD)
    public ResponseEntity<String> changePassword(@PathVariable("organizationId") Long organizationId, String password) {
        iamUserService.changePassword(password);
        return Results.success("success");
    }

    @GetMapping("/page")
    public ResponseEntity<Page<IamUser>> selectIamUserPage(@PathVariable("organizationId") Long organizationId, Page<IamUser> iamUserPage, IamUser iamUser) {
        iamUser.setTenantId(organizationId);
        return Results.success(iamUserService.selectIamUserPage(iamUserPage,iamUser));
    }

    @PostMapping("/change-password/verify-code")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.REGISTER)
    public ResponseEntity<String> changePasswordByVerifyCode(@PathVariable("organizationId") Long organizationId,@RequestBody ForgetPasswordDto forgetPasswordDto) {
        iamUserService.changePassword(forgetPasswordDto);
        return Results.success("success");
    }

    @PostMapping("/forget/verify-code")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.REGISTER)
    public ResponseEntity<String> sendForgetVerifyCode(@PathVariable("organizationId") Long organizationId, String email) {
        iamUserService.sendForgetVerifyCode(email);
        return Results.success("success");
    }

    @PostMapping("/forget/verify-code/validate")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.REGISTER)
    public ResponseEntity<Boolean> validateForgetVerifyCode(@PathVariable("organizationId") Long organizationId, String email, String verifyCode) {
        return Results.success(iamUserService.validateForgetVerifyCode(email, verifyCode));
    }

    @PostMapping("/register")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.REGISTER)
    public ResponseEntity<String> register(@PathVariable("organizationId") Long organizationId, @RequestBody IamUserRegisterDto iamUser) {
        iamUser.setTenantId(organizationId);
        iamUserService.registerUser(iamUser);
        return Results.success("success");
    }


    @PostMapping("/register/verify-code")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.REGISTER)
    public ResponseEntity<String> sendRegisterVerifyCode(@PathVariable("organizationId") Long organizationId, String email) {
        iamUserService.sendRegisterVerifyCode(email);
        return Results.success("success");
    }


    @PostMapping("/register/verify-code/validate")
    @KnifeInterfaceLog(interfaceEnum = KnifeInterfaceCodeEnum.REGISTER)
    public ResponseEntity<Boolean> validateRegisterVerifyCode(@PathVariable("organizationId") Long organizationId, String email, String verifyCode) {
        return Results.success(iamUserService.validateRegisterVerifyCode(email, verifyCode));
    }

}
